//
//  KMMessagView.m
//  KMMessageBox
//
//
#import "KMMessagView.h"
#import "KMPlaceholderTextView.h"
@interface KMMessagView()
{
    float superHeight;
    CGFloat maxy;//goodmessagebox的最大y值
}

@property (nonatomic,copy)void(^sendText)(NSString *);
@end
@implementation KMMessagView
-(instancetype)initWithFrame:(CGRect)frame PlaceText:(NSString *)placeText PlaceColor:(UIColor *)placeColor{
    self=[super initWithFrame:frame];
    if (self) {
        //添加顶分割线
       [self addSubview:[self addUpline]];
        self.backgroundColor = [UIColor whiteColor];
        //添加输入框
        CGRect textFrame=CGRectMake(12, 6,self.frame.size.width-24, self.height-12);
        _Inputview=[[KMPlaceholderTextView alloc]initWithFrame:textFrame PlaceText:placeText PlaceColor:placeColor];
        _Inputview.delegate=self;
        _Inputview.layer.borderColor = UIColorFromRGB(0xdcdcdc).CGColor;
        _Inputview.layer.borderWidth = 1;
        _Inputview.layer.cornerRadius = 4;
        [_Inputview TextViewSizeChange:^(CGSize size) {
            [self TextViewDidChange:size];
            
        }];
        _Inputview.returnKeyType=UIReturnKeyDone;
        _Inputview.font=[UIFont systemFontOfSize:14.0];
        [self addSubview:_Inputview];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShowAction:)
                                                     name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHideAction:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardDidShow)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        superHeight = frame.origin.y;
        maxy=superHeight;
        self.frame=CGRectMake(0, self.frame.origin.y-self.frame.size.height, self.frame.size.width, self.frame.size.height);//在底部显示

    }
    
    return self;
}

#pragma mark 添加上边line
-(UIView *)addUpline{
    CGRect frame=CGRectMake(0, -2, CGRectGetWidth([UIScreen mainScreen].bounds), 3);
    UIView *line=[[UIView alloc]initWithFrame:frame];
    [line setBackgroundColor:UIColorFromRGB(0xf0f0f0)];
    return line;
}

#pragma mark 处理来自输入框的大小
-(void)TextViewDidChange:(CGSize)size{
    self.frame=CGRectMake(0, self.frame.origin.y, self.frame.size.width,size.height+6*2);
    self.frame=CGRectMake(0,maxy-self.frame.size.height, self.frame.size.width, self.frame.size.height);
    _Inputview.frame=CGRectMake(12, 6, self.frame.size.width-24, size.height);
}
#pragma mark sendbutton发送
-(void)sendMessage:(void (^)(NSString * text))inputText{
    if (inputText) {
        self.sendText=inputText;
    }
}
-(void)buttonClick{
    [self sendInputText];
}
-(void)sendInputText{
    self.sendText(_Inputview.text);
//    if (_Inputview.Textnil) {
//        return;
//    }else{
//        if(self.sendText){
//        self.sendText(_Inputview.text);
////        _Inputview.text=@"";
//        }
//    }
}
#pragma mark - Keybaord Show Hide Notification

- (void)keyboardWillChangeFrameAction:(NSNotification*)aNotification {
}

- (void)keyboardWillShowAction:(NSNotification *)notification {
    NSDictionary *userInfo=[notification userInfo];
    NSTimeInterval boardAnimationDuration=[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame=[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    [UIView animateWithDuration:boardAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        CGFloat keyBoardHeigh=frame.size.height;
        maxy=superHeight-keyBoardHeigh;
        CGRect frame=self.frame;
        frame.origin.y=maxy-self.height;
        self.frame=frame;
        
    } completion:^(BOOL finished) {
        nil;
    }];
}

- (void)keyboardWillHideAction:(NSNotification *)notification {
//    self.isKeyboardShow = NO;
    NSDictionary *userInfo=[notification userInfo];
    NSTimeInterval boardAnimationDuration=[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:boardAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        maxy=superHeight;
        CGRect frame=self.frame;
        frame.origin.y=maxy-self.height;
        self.frame=frame;
        
    } completion:^(BOOL finished) {
        nil;
    }];
}

- (void)keyboardDidShow {
//    self.isKeyboardShow = YES;
}

#pragma mark 处理退出键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqual:@"\n"]) {
         [self sendInputText];
//        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
@end
